-
Notifications
You must be signed in to change notification settings - Fork 143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Automate C code formatting [1/3] #395
Conversation
Let's introduce an automatic code style formatting and checks for Themis. We use clang-format and clang-tidy tools as they are generally available and gives reasonable results. Introduce two new targets for Makefile: - make fmt - make fmt_check "fmt" is intended for humans to reformat the code before submitting changes for review. Currently it formats only C code but it may be extended to other languages as well. "fmt_check" is intended for CI to do format checks to ensure that the code is formatted accordingly. Unfortunately, clang-format does not have a convenient check mode so we hack one with git diff. clang-tidy requires the compilation flags so we integrate the format checking into the build pipeline. It's a bit repetetive, but also gives up a chance to cleanup the build system. And it uses Make features nicely so we don't have to recheck and reformat the files which are okay. We start by formatting only Soter and Themis source code. Other parts will be added later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
@Lagovas voiced some concerns about readability and maintainability of these cryptic command lines, so here's a translation from Perl to Human:
First we replace the variables:
Some options that we use:
We silence the stderr of clang-tidy because it likes to print silly statistics there which we don't need, but it looks like an error even if the file is okay. This spell: clang-format some/file | diff -u some/file - compares the original file with output of clang-format (formatted file) that is printed to stdout. The Finally, we touch a marker file to tell Make that the source file has been successfully processed. With that the formatting will not be made again on subsequent calls to make. I hope this helped you, lost soul who came to this PR searching for wisdom of the ancients after looking into |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great!
thanks for explanation : ) |
In light of all this I would rather that these targets be ephemeral (without the marker files). Unless, you have some other use-case in mind |
Rest assured, I'll rename the resulting commits when merging these branches.
I left the diff output so that when the check fails on the CI then the build logs will immediately tell the dev what's wrong with the code style. The same goes for the developer manually checking the code with
First of all, they allow us to hook those custom Then they allow us to avoid reformatting the files which were already formatted. Keep in mind that make tracks the modification time of the files, and the marker files are created after the source files have been changed by the formatting tools. Thus we avoid reformatting the files that were not changed since they were formatted right. If you first run |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that makes sense
Let's introduce an automatic code style formatting and checks for Themis. We use clang-format and clang-tidy tools as they are generally available and gives reasonable results.
Introduce two new targets for Makefile:
fmt is intended for humans to reformat the code before submitting changes for review. Currently it formats only C code but it may be extended to other languages as well.
fmt_check is intended for CI to do format checks to ensure that the code is formatted accordingly. Unfortunately, clang-format does not have a convenient check mode so we hack one with git diff.
clang-tidy requires the compilation flags so we integrate the format checking into the build pipeline. It's a bit repetetive, but also gives up a chance to cleanup the build system. And it uses Make features nicely so we don't have to recheck and reformat the files which are okay.
We start by formatting only Soter and Themis source code. Other parts will be added later.
Supersedes #391, relates to #270